<?php

namespace App\Http\Controllers\RangingList;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use App\Http\Controllers\Controller;
use App\Message;
use App\Post;
use App\user;
use App\Http\Requests\operationProjectMessageRequest;
use App\Http\Requests\sendGeneralRequest;
use App\Http\Requests\searchingGeneralRequest;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;


class RangingListController extends Controller
{
    //获取基本信息
    public function getProjectMessage(Request $request){
       $data = DB::table('posts')
            ->join('users', 'users.id', '=', 'posts.user_id')
            ->select(DB::raw('users.id, users.account,users.user_name,sum(posts.like) as tol_dz ,count(posts.user_id) as tol_article,users.rating'))
            ->groupBy('posts.user_id')
            ->orderBy('users.rating', 'desc')
            ->paginate(8);
        if ($data){
            return response()->success(200,'成功',$data);
        }
        return response()->fail(100,'失敗');

    }
    //查询用户所有发布的文章信息
    public function operationProjectMessage(Request $request)
    {
        $id = $request->id;
        $data = DB::table('users')->join('posts', 'users.id', 'posts.user_id')->where('users.id', $id)->select('posts.id','title', 'like', 'user_name', 'posts.created_at', 'view')->orderBy('posts.created_at', 'desc')->paginate(8);
        if ($data) {
            return response()->success(200, '成功', $data);
        }
        return response()->fail(100, '失敗');
    }

    //获取反馈信息
    public function sendGeneral(sendGeneralRequest $request)
    {
        $message = new Message();
        $message->pull_id = $request->pull_id;
        $message->post_id = $request->post_id;
        $message->push_id = $request->push_id;
        $message->status = $request->status;
        $message->read = $request->read;
        $message->msg = $request->msg;
        $message->feedback = $request->input('feedback');
        $result = $message->save();
        if ($result) {
            return response()->success(200, '成功', $result);
        }
        return response()->fail(100, '失敗');
    }
    //查询具体用户的信息
    
     public function searchingGeneral(Request $request)
    {
        $keywords = $request->user_name;

       $data = DB::table('posts')
            ->join('users', 'users.id', '=', 'posts.user_id')
            ->where('user_name', 'like', '%' . $keywords . '%')
            ->select(DB::raw('users.id,users.account,users.user_name,sum(posts.like) as tol_dz ,count(posts.user_id) as tol_article,users.rating'))
            ->groupBy('posts.user_id')
            ->paginate(8);
        if ($data){
            return response()->success(200,'成功',$data);
        }
        return response()->fail(100,'失敗');
    }
}
